home *** CD-ROM | disk | FTP | other *** search
/ Macworld Expo - Develope…Central & Net Innovations / Developer Central and Net Innovators (MacWorld Expo) (January 1999).iso / Developer Central / Bowers Development / Demo AppMaker / Examples / plain C OS8 / Temperature / MainWindow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-29  |  8.3 KB  |  389 lines  |  [TEXT/CWIE]

  1. /* MainWindow.c */
  2.  
  3. #include <Types.h>
  4. #include <Quickdraw.h>
  5. #include <Controls.h>
  6. #include <Dialogs.h>
  7. #include <Events.h>
  8. #include <Lists.h>
  9. #include <Menus.h>
  10. #include <Resources.h>
  11. #include <Sound.h>
  12. #include <TextEdit.h>
  13. #include <ToolUtils.h>
  14. #include <Appearance.h>
  15. #include <stdlib.h>
  16.  
  17. #include "Globals.h"
  18. #include "ResourceDefs.h"
  19. #include "DoScrap.h"
  20. #include "Miscellany.h"
  21. #include "Scrolling.h"
  22. #include "ControlUtils.h"
  23. #include "Dispatcher.h"
  24. #include "DDocData.h"
  25. #include "TemperatureEngine.h"
  26. #include "TemperatureDoc.h"
  27. #include "MainWindow.h"
  28.  
  29.  
  30. static    TemperatureEngine*      GetEngine    (MainWindow*    self);
  31.  
  32. static    void    DoUndo        (MainWindow*    self);
  33. static    void    DoCut        (MainWindow*    self);
  34. static    void    DoCopy        (MainWindow*    self);
  35. static    void    DoPaste        (MainWindow*    self);
  36. static    void    DoClear        (MainWindow*    self);
  37. static    void    DoSelectAll        (MainWindow*    self);
  38. static    void    DoShowClipboard    (MainWindow*    self);
  39.  
  40.  
  41. //----------
  42. MainWindow*        NewMainWindow ()
  43. {
  44.     MainWindow*        window;
  45.  
  46.     window = (MainWindow*)malloc (sizeof (MainWindow));
  47.     MainWindow_Init (window);
  48.     SetClassID (window, classMainWindow);
  49.  
  50.     return window;
  51. }
  52.  
  53. //----------
  54. void    DeleteMainWindow (
  55.     MainWindow*        window)
  56. {
  57.     MainWindow_Free (window);
  58.     free (window);
  59. }
  60.  
  61. //----------
  62. void    MainWindow_Create (
  63.     AMDoc*            inDoc,
  64.     DDocData*        inData)
  65. {
  66.     MainWindow*        winObj = NewMainWindow ();
  67.  
  68.     if (winObj != nil) {
  69.         MainWindow_Open (winObj, inDoc, inData);
  70.     }
  71. }
  72.  
  73. //----------
  74. void    MainWindow_Init (
  75.     MainWindow*        self)
  76. {
  77. }
  78.  
  79. //----------
  80. void    MainWindow_Free (
  81.     MainWindow*        self)
  82. {
  83. }
  84.  
  85. //----------
  86. TemperatureEngine*    GetEngine (
  87.     MainWindow*        self)
  88. {
  89.     return (TemperatureEngine*) self->super.mDoc->mEngine;
  90. }
  91.  
  92. /*----------*/
  93. void    MainWindow_Open (
  94.     MainWindow*        self,
  95.     AMDoc*            inDoc,
  96.     DDocData*        inData)
  97. {
  98.     WindowPtr        window;
  99.     Handle            wftb;
  100.  
  101.     self->super.mDoc = inDoc;
  102.     self->mData = inData;
  103.     AddResponder ((AMSignaler*) self->mData, (AMResponder*) self);
  104.  
  105.     window = GetNewCWindow (WIND_MainWindow, nil, (WindowPtr) -1L);
  106.     if (AMEngine_GetFilename (self->super.mDoc->mEngine) [0] != 0) {
  107.         SetWTitle (window, AMEngine_GetFilename (self->super.mDoc->mEngine));
  108.     }
  109.     self->super.mWindow = window;
  110.     ((TemperatureDoc*)self->super.mDoc)->mMainWindowPtr = window;
  111.  
  112.     SetWindowKind (window, 'AM');
  113.     SetWRefCon (window, (long) self);
  114.     SetPort (window);
  115.     SetInfo (window);
  116.  
  117.     wftb = GetResource ('Wftb', WIND_MainWindow);
  118.  
  119.     CreateRootControl (window, &self->super.mRootControl);
  120.  
  121.     self->super.vScroll = nil;
  122.     self->super.hScroll = nil;
  123.  
  124.  
  125.     self->mCentigradeLabel = GetNewControl (CNTL_Centigrade, window);
  126.     SetWindowItemFont (self->mCentigradeLabel, wftb, 1);
  127.     SetControlFromTEXT (self->mCentigradeLabel, TEXT_Centigrade);
  128.  
  129.     self->mEditCentigradeHandle = GetNewControl (CNTL_EditCentigrade, window);
  130.     SetWindowItemFont (self->mEditCentigradeHandle, wftb, 2);
  131.     SetControlTextValue (self->mEditCentigradeHandle, GetCentigrade (self->mData));
  132.  
  133.     self->mFahrenheitLabel = GetNewControl (CNTL_Fahrenheit, window);
  134.     SetWindowItemFont (self->mFahrenheitLabel, wftb, 3);
  135.     SetControlFromTEXT (self->mFahrenheitLabel, TEXT_Fahrenheit);
  136.  
  137.     self->mEditFahrenheitHandle = GetNewControl (CNTL_EditFahrenheit, window);
  138.     SetWindowItemFont (self->mEditFahrenheitHandle, wftb, 4);
  139.     SetControlTextValue (self->mEditFahrenheitHandle, GetFahrenheit (self->mData));
  140.  
  141.     self->mCentSliderHandle = GetNewControl (CNTL_CentSlider, window);
  142.     SetWindowItemFont (self->mCentSliderHandle, wftb, 5);
  143.     SetControlValue (self->mCentSliderHandle, GetCentigrade (self->mData));
  144.  
  145.     self->mFahrBarHandle = GetNewControl (CNTL_FahrBar, window);
  146.     SetWindowItemFont (self->mFahrBarHandle, wftb, 6);
  147.     SetControlValue (self->mFahrBarHandle, GetFahrenheit (self->mData));
  148.  
  149.     AdvanceKeyboardFocus (window);
  150.  
  151.     ShowWindow (window);
  152. }
  153.  
  154. /*----------*/
  155. void    MainWindow_Close (
  156.     MainWindow*        self)
  157. {
  158.     RemoveResponder ((AMSignaler*) self->mData, (AMResponder*) self);
  159.  
  160.     ((TemperatureDoc*)self->super.mDoc)->mMainWindowPtr = nil;
  161.     SetInfo (nil);
  162.     HideWindow (self->super.mWindow);
  163.     DisposeWindow (self->super.mWindow);
  164.  
  165.     DeleteMainWindow (self);
  166. }
  167.  
  168. /*----------*/
  169. void    MainWindow_Track (
  170.     MainWindow*        self,
  171.     ControlHandle    whichControl,
  172.     short            whichPart,
  173.     Point            where)
  174. {
  175.     Rect            bounds;
  176.     short            newValue;
  177.  
  178.     if (whichControl == self->mEditCentigradeHandle) {
  179.         AMWindow_HandleEditClick ((AMWindow*) self, self->mEditCentigradeHandle, where);
  180.     }
  181.     if (whichControl == self->mEditFahrenheitHandle) {
  182.         AMWindow_HandleEditClick ((AMWindow*) self, self->mEditFahrenheitHandle, where);
  183.     }
  184.     if (whichControl == self->mCentSliderHandle) {
  185.         HandleControlClick (self->mCentSliderHandle, where, curEvent.modifiers, nil);
  186.         SetCentigrade (self->mData, GetControlValue (self->mCentSliderHandle));
  187.     }
  188. }
  189.  
  190. //----------
  191. void    MainWindow_DataChanged (
  192.     MainWindow*        self,
  193.     long            inDataID)
  194. {
  195.     if (inDataID == idCentigrade) {
  196.         SetControlTextValue (self->mEditCentigradeHandle, GetCentigrade (self->mData));
  197.     }
  198.     if (inDataID == idFahrenheit) {
  199.         SetControlTextValue (self->mEditFahrenheitHandle, GetFahrenheit (self->mData));
  200.     }
  201.     if (inDataID == idCentigrade) {
  202.         SetControlValue (self->mCentSliderHandle, GetCentigrade (self->mData));
  203.     }
  204.     if (inDataID == idFahrenheit) {
  205.         SetControlValue (self->mFahrBarHandle, GetFahrenheit (self->mData));
  206.     }
  207. }
  208.  
  209. /*----------*/
  210. void    MainWindow_MouseIn (
  211.     MainWindow*        self,
  212.     Point            where,
  213.     short            modifiers)
  214. {
  215.     Rect        bounds;
  216.  
  217. }
  218.  
  219. //----------
  220. void    MainWindow_ExitCurField (
  221.     MainWindow*        self)
  222. {
  223.     ControlHandle    focus;
  224.  
  225.     GetKeyboardFocus (self->super.mWindow, &focus);
  226.  
  227.     if (focus == nil) {
  228.         // nothing to exit
  229.  
  230.     } else if (focus == self->mEditCentigradeHandle) {
  231.         SetCentigrade (self->mData, GetControlTextValue (self->mEditCentigradeHandle));
  232.     } else if (focus == self->mEditFahrenheitHandle) {
  233.         SetFahrenheit (self->mData, GetControlTextValue (self->mEditFahrenheitHandle));
  234.     }
  235. }
  236.  
  237. /*----------*/
  238. void    MainWindow_TypeIn (
  239.     MainWindow*        self,
  240.     char            ch)
  241. {
  242.     ControlHandle    focus;
  243.     short            keyCode;
  244.  
  245.     GetKeyboardFocus (self->super.mWindow, &focus);
  246.  
  247.     if ((ch == charEnter)
  248.     ||  (ch == charReturn)) {
  249.         MainWindow_ExitCurField (self);    // Dispatch
  250.     } else if (ch == charEsc) {
  251.     } else if (ch == charTab) {
  252.         AMWindow_DoTab ((AMWindow*) self, (curEvent.modifiers & optionKey) != 0);
  253.     } else if (focus != nil) {
  254.         keyCode = curEvent.message & keyCodeMask;
  255.         HandleControlKey (focus, keyCode, ch, curEvent.modifiers);
  256.         AMEngine_SetDirty (self->super.mDoc->mEngine);
  257.     } else {
  258.         SysBeep (1);
  259.     }
  260. }
  261.  
  262. /*----------*/
  263. void    MainWindow_Resize (
  264.     MainWindow*        self)
  265. {
  266.     /* application-specific code to resize items in window */
  267. }
  268.  
  269. /*----------*/
  270. void    MainWindow_Scroll (
  271.     MainWindow*        self,
  272.     short            newValue,
  273.     short            oldValue)
  274. {
  275.     /* application-specific code to scroll window */
  276.     if (gWhichScroll == self->super.vScroll) {
  277.     } else {    // horizontal
  278.     }
  279. }
  280.  
  281. //----------
  282. void    DoUndo (
  283.     MainWindow*        self)
  284. {
  285. } // DoUndo
  286.  
  287. //----------
  288. void    DoCut (
  289.     MainWindow*        self)
  290. {
  291.     TEHandle        curTE = AMWindow_GetCurTE ((AMWindow*) self);
  292.  
  293.     if (curTE != nil) {
  294.         TECut (curTE);
  295.         AMEngine_SetDirty (self->super.mDoc->mEngine);
  296.         scrapDirty = true;
  297.     }
  298. } // DoCut
  299.  
  300. //----------
  301. void    DoCopy (
  302.     MainWindow*        self)
  303. {
  304.     TEHandle        curTE = AMWindow_GetCurTE ((AMWindow*) self);
  305.  
  306.     if (curTE != nil) {
  307.         TECopy (curTE);
  308.         scrapDirty = true;
  309.     }
  310. } // DoCopy
  311.  
  312. //----------
  313. void    DoPaste (
  314.     MainWindow*        self)
  315. {
  316.     TEHandle        curTE = AMWindow_GetCurTE ((AMWindow*) self);
  317.  
  318.     if (curTE != nil) {
  319.         TEPaste (curTE);
  320.         AMEngine_SetDirty (self->super.mDoc->mEngine);
  321.     }
  322. } // DoPaste
  323.  
  324. //----------
  325. void    DoClear (
  326.     MainWindow*        self)
  327. {
  328.     TEHandle        curTE = AMWindow_GetCurTE ((AMWindow*) self);
  329.  
  330.     if (curTE != nil) {
  331.         TEDelete (curTE);
  332.         AMEngine_SetDirty (self->super.mDoc->mEngine);
  333.     }
  334. } // DoClear
  335.  
  336. //----------
  337. void    DoSelectAll (
  338.     MainWindow*        self)
  339. {
  340.     TEHandle        curTE = AMWindow_GetCurTE ((AMWindow*) self);
  341.  
  342.     if (curTE != nil) {
  343.         TESetSelect (0, 32767, curTE);
  344.     }
  345. } // DoSelectAll
  346.  
  347. //----------
  348. void    DoShowClipboard (
  349.     MainWindow*        self)
  350. {
  351. } // DoShowClipboard
  352.  
  353. //----------
  354. Boolean        MainWindow_DoCommand (
  355.     MainWindow*        self,
  356.     long            inCommand)
  357. {
  358.     Boolean        result = true;
  359.  
  360.     switch (inCommand) {
  361.         case cmdUndo:
  362.                 DoUndo (self);
  363.             break;
  364.         case cmdCut:
  365.                 DoCut (self);
  366.             break;
  367.         case cmdCopy:
  368.                 DoCopy (self);
  369.             break;
  370.         case cmdPaste:
  371.                 DoPaste (self);
  372.             break;
  373.         case cmdClear:
  374.                 DoClear (self);
  375.             break;
  376.         case cmdSelectAll:
  377.                 DoSelectAll (self);
  378.             break;
  379.         case cmdShowClipboard:
  380.                 DoShowClipboard (self);
  381.             break;
  382.  
  383.         default:
  384.                 result = false;
  385.     } // switch
  386.  
  387.     return result;
  388. }
  389.